/*
 * Created on 2005-12-16
 *
 */
package bma.common.langutil.core;

import java.io.InputStream;
import java.net.URL;

/**
 * 资源工具类
 * 
 * @author 关中
 * @since 1.0 (imported from BMA.ESP Project)
 * 
 */
public class ResourceUtil {

	public static String name2path(String name, boolean root) {
		if (name == null)
			return null;
		StringBuffer buf = new StringBuffer(name.length() + 1);
		if (root)
			buf.append('/');
		buf.append(name.replace('.', '/'));
		return buf.toString();
	}

	public static String name2path(Class<?> cls, boolean root) {
		return name2path(cls.getName(), root);
	}

	public static String getNameFromClass(Class<?> refClass, String name,
			boolean root) {
		VPath path = VPath.create(ResourceUtil.name2path(refClass.getName(),
				true));
		path = path.setName(name);
		return path.toString(root);
	}

	public static URL getResourceFromClass(Class<?> refClass, String name) {
		VPath path = VPath.create(ResourceUtil.name2path(refClass.getName(),
				true));
		path = path.setName(name);
		return Thread.currentThread().getContextClassLoader()
				.getResource(path.toString(false));
	}

	public static InputStream getResourceAsStream(String name) {
		return Thread.currentThread().getContextClassLoader()
				.getResourceAsStream(name);
	}

}
